const sql = require('mssql');
const baseFn = require('../../public/base.js');



// 获取分类信息
const getAssetClassifyInfo = async (ctx, next) => {

    let selectSql = `SELECT * FROM ZCFLGL`;

    let result = await sql.query(selectSql);
    ctx.body = { result: result.recordset };
}
// 添加资产下面的多级名称信息
const addAssetName = async (ctx, next) => {
    let getBody = ctx.request.body;
    let actionSql = '';
    if (('btn' in getBody.parantInfo) && getBody.parantInfo.btn === 'edit') {
        // actionSql = `UPDATE ZCMCGL SET name = '${getBody.name}',category = ${getBody.category} WHERE id = ${getBody.parantInfo.id}`;
        actionSql = `UPDATE ZCMCGL SET name = '${getBody.name}'  WHERE id = ${getBody.parantInfo.id}`;
 
    } else {
        // actionSql = `INSERT INTO ZCMCGL(name,type,pid,status,category,classifyId) VALUES ('${getBody.name}',${('type' in getBody.parantInfo) ? (Number(getBody.parantInfo.type) + 1) : 1},${('pid' in getBody.parantInfo) ? (getBody.parantInfo.id) : 0},'T',${getBody.category},${getBody.chooseClassifyVal.id} )`;
        actionSql = `INSERT INTO ZCMCGL(name,type,pid,status,classifyId) VALUES ('${getBody.name}',${('type' in getBody.parantInfo) ? (Number(getBody.parantInfo.type) + 1) : 1},${('pid' in getBody.parantInfo) ? (getBody.parantInfo.id) : 0},'T',${getBody.chooseClassifyVal.id} )`;
 
    }
    let result = await sql.query(actionSql);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}

// 添加资产分类信息
const addAssetClassify = async (ctx, next) => {
    let getBody = ctx.request.body;
    let actionSql = '';
    if ('id' in getBody) {
        actionSql = `UPDATE ZCFLGL SET name = '${getBody.name}' WHERE id = ${getBody.id}`;
    } else {
        actionSql = `INSERT INTO ZCFLGL(name,status) VALUES ('${getBody.name}','T' )`;
    }
    let result = await sql.query(actionSql);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}

// 改变分类的使用状态
const changeClassifyStauts = async (ctx, next) => {
    let getBody = ctx.request.body;

    let result = await sql.query(`UPDATE ZCFLGL SET status = '${getBody.status === 'T' ? 'F' : 'T'}' WHERE id = ${getBody.id}`);
    sql.query(`UPDATE ZCMCGL SET status = '${getBody.status === 'T' ? 'F' : 'T'}' WHERE classifyId = ${getBody.id}`);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}
// 获取资产分类下面的多级名称的信息

const getAssetNameInfo = async (ctx, next) => {
    let getBody = ctx.request.body;
    let result = await sql.query(`SELECT * FROM  ZCMCGL  WHERE classifyId = ${getBody.id}`);
    ctx.body = { result: result.recordset };
}
// 改变分类下面的名称的使用状态
const changeAssetNameStauts = async (ctx, next) => {
    let getBody = ctx.request.body;
    getBody.forEach(async item => {
        await sql.query(`UPDATE ZCMCGL SET status = '${getBody[0].status === 'T' ? 'F' : 'T'}' WHERE id = ${item.id}`);
    });
    // let result = await sql.query(actionSql);
    // if (result.rowsAffected[0] > 0) {
    ctx.body = {
        success: true
    }
    // } else {
    //     ctx.body = {
    //         success: false
    //     }
    // }
}


// 添加资产分类信息
const addAssetConfig = async (ctx, next) => {
    let getBody = ctx.request.body;
    let actionSql = '';
    if ('id' in getBody.configInfo) {
        actionSql = `UPDATE ZCPZGL SET name = '${getBody.configInfo.name}' WHERE id = ${getBody.configInfo.id}`;
    } else {
        actionSql = `INSERT INTO ZCPZGL(name,status,nameId) VALUES ('${getBody.configInfo.name}','T',${getBody.chooseName} )`;
    }
    let result = await sql.query(actionSql);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}

// 获取资产分类下面的配置的信息

const getAssetConfigInfo = async (ctx, next) => {
    let getBody = ctx.request.body;
    let result = await sql.query(`SELECT * FROM  ZCPZGL  WHERE nameId = ${getBody.id}`);
    ctx.body = { result: result.recordset };
}
// 改变资产配置的启用和禁用的状态
const changeConfigStauts = async (ctx, next) => {
    let getBody = ctx.request.body;
    actionSql = `UPDATE ZCPZGL SET status = '${getBody.status === 'T' ? 'F' : 'T'}' WHERE id = ${getBody.id}`;
    let result = await sql.query(actionSql);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}


module.exports = {
    'POST /changeAssetNameStauts': changeAssetNameStauts,
    'POST /addAssetClassify': addAssetClassify,
    'POST /changeClassifyStauts': changeClassifyStauts,
    'POST /addAssetName': addAssetName,
    'POST /getAssetNameInfo': getAssetNameInfo,
    'POST /getAssetConfigInfo': getAssetConfigInfo,
    'POST /addAssetConfig': addAssetConfig,
    'POST /changeConfigStauts': changeConfigStauts,
    
    'GET /getAssetClassifyInfo': getAssetClassifyInfo
}